import greenfoot.*;  
import java.util.*;

/**
 * This class represents the breach in the sandbags game
 * 
 * @author Jasper Jans
 */
public class Breach extends Actor
{

    private long timeStarted = System.currentTimeMillis();  
    private int secondsCounter = 0;

    // Breached columns
    private List<Sandbag> col1 = new ArrayList<Sandbag>();
    private List<Sandbag> col2 = new ArrayList<Sandbag>();
    private List<Sandbag> col3 = new ArrayList<Sandbag>();
    private List<Sandbag> col4 = new ArrayList<Sandbag>();
    
    private int waveNum = 1;
    
    private SandBags sandBagsWorld;
    
    public Breach(SandBags sandBagsWorld)
    {
        this.sandBagsWorld = sandBagsWorld;
    }
    
    /*
     * Stop sandbags in given column with blinking
     */
    private void stopBlinkingSandbags(List<Sandbag> column)
    {
        for(int i = 0;i< column.size(); i++)
            column.get(i).stopBlinking();
    }
    
    /*
     * Adds the sandbag to the specified columns
     */
    private void addSandBagToColumn(List<Sandbag> column, Sandbag sandbag, int x)
    {
        stopBlinkingSandbags(column); 
        column.add(sandbag);
        sandbag.setLocation(getX() - (x  - column.size() * 2), getY() - (column.size() * 10));
        sandbag.small();      
        
        checkUnBreached();  // check to see if this was last sandbag
    }
    
    /*
     * Checks to see if all columns have been filled with sandbags
     */
    private void checkUnBreached()
    {
        if(col1.size() == 3 && col2.size() == 3 && col3.size() == 3 && col4.size() == 3)
        {
            sandBagsWorld.gameWon();    // sandbagwall is completed!
        }
    }
    
    /*
     * Checks if sandbags is in a location which can be seen as a valid throw-result.
     * If so, add the sandbag to the column and return true
     */
    public Boolean checkSandBagLocation(Sandbag sandbag)
    {
        if(sandbag.getY() > getY())
            return false;
        
        int x = sandbag.getX();
        
        if(x >= getX() - 75 && x <= getX() - 25)    // thrown between 325 and 275
        {
            if(col1.size() < 3)
            {
                addSandBagToColumn(col1, sandbag, 50);
                return true;
            }
        }
        
        if(x >= getX() - 40 && x <= getX() + 10)  // thrown between 360 and 410
        {
            if(col2.size() < 3)
            {
                addSandBagToColumn(col2, sandbag, 20);
                return true;                
            }
        }

        if(x >= getX() - 5 && x <= getX() + 45) // thrown between 395 and 445
        {
            if(col3.size() < 3)
            {
                addSandBagToColumn(col3, sandbag, -10);
                return true;
            }
        }        
        
       if(x >= getX() + 30 && x <= getX() + 80) // thrown between 430 and 480
        {
            if(col4.size() < 3)
            {
                addSandBagToColumn(col4, sandbag, -45);
                return true;
            }
        }         
        
        return false;
    }
    

    /*
     * Decreases sandbag-filling every second and removed if no filling is left
     * Generates a new wave every two seconds for all columns that have less than 2 sandbags
     */
    public void act() 
    {

        long currentTime = System.currentTimeMillis();
        long elapsedTime = currentTime - timeStarted;
     
        
        if(sandBagsWorld.getGameStatus() != GameStatus.WON && elapsedTime / 1000 >= 1)
        {
            secondsCounter++;
            
            if(col1.size() < 3 || col2.size() < 3 || col3.size() < 3 || col3.size() < 3)
            {
                
                if(col1.size() > 0){
                    if(col1.get(col1.size() -1).emptyMeAndRemove())
                    {
                        col1.remove(col1.size()-1);
                    }
                }
                
                if(col2.size() > 0){
                    if(col2.get(col2.size() -1).emptyMeAndRemove())
                    {
                        col2.remove(col2.size()-1);
                    }
                }
                if(col3.size() > 0){
                    if(col3.get(col3.size() -1).emptyMeAndRemove())
                    {
                        col3.remove(col3.size()-1);
                    }
                }
                if(col4.size() > 0){
                    if(col4.get(col4.size() -1).emptyMeAndRemove())
                    {
                        col4.remove(col4.size()-1);
                    }
                }            
            }
        
            if(secondsCounter == 2 )
            {
                GreenfootSound wave = new GreenfootSound("sandbags-wave01.mp3");
                wave.setVolume(30);
                wave.play();
                    
                if(col1.size() < 2)
                {
                    Water w1 = new Water(true, waveNum);
                    getWorld().addObject(w1, getX() -50, getY() + 30);
                }
                
                if(col2.size() < 2)
                {
                    Water w2 = new Water(true, waveNum);
                    getWorld().addObject(w2, getX()-10, getY() + 30);
                }
                
                if(col3.size() < 2)
                {
                    Water w3 = new Water(true, waveNum);
                    getWorld().addObject(w3, getX() + 30, getY() + 30);
                }
                
                if(col4.size() < 2)
                {
                    Water w4 = new Water(true, waveNum);
                    getWorld().addObject(w4, getX() + 70, getY() + 30);
                }
                
                waveNum++;
                secondsCounter = 0;
            }
            
            timeStarted = System.currentTimeMillis();  
      
        } 
        
 

    }    
}
